﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.DAL
{
    /// <summary>
    /// 数据访问类ChangeRateInfo。
    /// </summary>
    public class ChangeRateInfoDAL
    {
        public ChangeRateInfoDAL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sExistsWhere)
        {
            return SQLHelper.Exists("tb_ChangeRateInfo", sExistsWhere);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.ChangeRateInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_ChangeRateInfo(");
            strSql.Append("Years,Area_ID,EnergyChange,BaseChange,TrafficChange,Type)");
            strSql.Append(" values (");
            strSql.Append("@Years,@Area_ID,@EnergyChange,@BaseChange,@TrafficChange,@Type)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Years", SqlDbType.Char,4),
					new SqlParameter("@Area_ID", SqlDbType.Int,4),
					new SqlParameter("@EnergyChange", SqlDbType.Decimal,5),
					new SqlParameter("@BaseChange", SqlDbType.Decimal,5),
					new SqlParameter("@TrafficChange", SqlDbType.Decimal,5),
					new SqlParameter("@Type", SqlDbType.Char,1)};
            parameters[0].Value = model.Years;
            parameters[1].Value = model.Area_ID;
            parameters[2].Value = model.EnergyChange;
            parameters[3].Value = model.BaseChange;
            parameters[4].Value = model.TrafficChange;
            parameters[5].Value = model.Type;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.ChangeRateInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_ChangeRateInfo set ");
            strSql.Append("Years=@Years,");
            strSql.Append("Area_ID=@Area_ID,");
            strSql.Append("EnergyChange=@EnergyChange,");
            strSql.Append("BaseChange=@BaseChange,");
            strSql.Append("TrafficChange=@TrafficChange,");
            strSql.Append("Type=@Type");
            strSql.Append(" where CRI_ID=@CRI_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CRI_ID", SqlDbType.Int,4),
					new SqlParameter("@Years", SqlDbType.Char,4),
					new SqlParameter("@Area_ID", SqlDbType.Int,4),
					new SqlParameter("@EnergyChange", SqlDbType.Decimal,5),
					new SqlParameter("@BaseChange", SqlDbType.Decimal,5),
					new SqlParameter("@TrafficChange", SqlDbType.Decimal,5),
					new SqlParameter("@Type", SqlDbType.Char,1)};
            parameters[0].Value = model.CRI_ID;
            parameters[1].Value = model.Years;
            parameters[2].Value = model.Area_ID;
            parameters[3].Value = model.EnergyChange;
            parameters[4].Value = model.BaseChange;
            parameters[5].Value = model.TrafficChange;
            parameters[6].Value = model.Type;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string sIDs)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_ChangeRateInfo ");
            strSql.Append(" where CRI_ID in ( " + sIDs + " )");
            return SQLHelper.ExecuteNonQuery(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.ChangeRateInfo GetModel(int CRI_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 CRI_ID,Years,Area_ID,EnergyChange,BaseChange,TrafficChange,Type from tb_ChangeRateInfo ");
            strSql.Append(" where CRI_ID=@CRI_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@CRI_ID", SqlDbType.Int,4)};
            parameters[0].Value = CRI_ID;

            HsTec.Mobile.Green.Model.ChangeRateInfo model = new HsTec.Mobile.Green.Model.ChangeRateInfo();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["CRI_ID"].ToString() != "")
                {
                    model.CRI_ID = int.Parse(ds.Tables[0].Rows[0]["CRI_ID"].ToString());
                }
                model.Years = ds.Tables[0].Rows[0]["Years"].ToString();
                if (ds.Tables[0].Rows[0]["Area_ID"].ToString() != "")
                {
                    model.Area_ID = int.Parse(ds.Tables[0].Rows[0]["Area_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EnergyChange"].ToString() != "")
                {
                    model.EnergyChange = decimal.Parse(ds.Tables[0].Rows[0]["EnergyChange"].ToString());
                }
                if (ds.Tables[0].Rows[0]["BaseChange"].ToString() != "")
                {
                    model.BaseChange = decimal.Parse(ds.Tables[0].Rows[0]["BaseChange"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TrafficChange"].ToString() != "")
                {
                    model.TrafficChange = decimal.Parse(ds.Tables[0].Rows[0]["TrafficChange"].ToString());
                }
                model.Type = ds.Tables[0].Rows[0]["Type"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CRI_ID,Years,Area_ID,EnergyChange,BaseChange,TrafficChange,Name,Type ");
            strSql.Append(" FROM vi_ChangeRateInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where (1=1) " + strWhere);
            }
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" CRI_ID,Years,Area_ID,EnergyChange,BaseChange,TrafficChange,Type ");
            strSql.Append(" FROM tb_ChangeRateInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where (1=1) " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        #endregion  成员方法
    }
}

